﻿using System;
using System.Collections.Generic;
using System.IO;
using System.Linq;
using System.Text;

namespace Parser
{
    class Program
    {
        static void Main(string[] args)
        {
            string file = @"H:\ITF\Project\IFLT\IFLT\bin\Debug\Versions\2.0.2\db\tank_model.db";

            string[] lines = File.ReadAllLines(file);
            string[] headerData;
            string[] detailData;
            List<string> elements;
            string item;
            string value;
            foreach (string line in lines)
            {
                switch (line[0])
                {
                    case '{':
                        break;

                    case '}':
                        break;

                    case '\t':
                        switch (line[1])
                        {
                            // Header
                            case '"':
                                elements = customSplit(line, ':');

                                headerData = line.Substring(0, line.Length - 2).Split(':');
                                if (headerData.Length == 1)
                                {
                                    // Data Detail
                                }
                                else
                                {
                                    // Data info
                                    item = headerData[0];
                                    value = headerData[1];

                                    if( item == "field")
                                    {
                                        elements = customSplit(value, ',');
                                    }

                                }
                                break;

                            case '[':
                                break;

                            case '\t':
                                elements = customSplit(line, ',');

                                detailData = line.Split(',');
                                foreach (string data in detailData)
                                {
                                    Console.WriteLine(data);
                                }
                                break;
                        }
                        break;
                }
            }
        }

        static List<string> customSplit(string input, char separator)
        {
            List<string> elements = new List<string>();
            StringBuilder elementBuilder = new StringBuilder();
            string aux;

            bool isQuoted = false;
            foreach (char c in input.ToCharArray())
            {
                if (c == '\"')
                {
                    isQuoted = !isQuoted;
                    // continue;        // changed according to the OP comment - \" shall not be skipped
                }
                if (c == separator && !isQuoted)
                {
                    aux = CleanElement(elementBuilder.ToString().Trim());
                    elements.Add(aux);
                    elementBuilder = new StringBuilder();
                    continue;
                }
                elementBuilder.Append(c);
            }

            aux = CleanElement(elementBuilder.ToString().Trim());
            if (!string.IsNullOrEmpty(aux))
            {
                elements.Add(aux);
            }
            return elements;
        }

        static string CleanElement(string element)
        {
            char[] remove = { '\t', '\"', '[', ']', ',' };
            string aux = element.Trim();

            while (aux.Length > 0 && remove.Contains(aux[0]))
            {
                aux = aux.Substring(1, aux.Length - 1);
            }

            while (aux.Length > 0 && remove.Contains(aux[aux.Length - 1]))
            {
                aux = aux.Substring(0, aux.Length - 1);
            }

            return aux;
        }
    }
}
